package middleware

import (
	"net/http"
	"strings"
)

func AddPrefix(prefix string, h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// 将前缀添加到请求的 URL 路径
		r.URL.Path = strings.Join([]string{prefix, r.URL.Path}, "")
		h.ServeHTTP(w, r)
	})
}

func UpdatePrefix(stripPrefix string, addPrefix string, h http.Handler) http.Handler {
	return http.StripPrefix(stripPrefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// 将前缀添加到请求的 URL 路径
		r.URL.Path = strings.Join([]string{addPrefix, r.URL.Path}, "")
		h.ServeHTTP(w, r)
	}))
}
